<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .container {
        width: 400px;
        height: 250px;
        border: 1px solid #000;
        margin: 50px auto;
        overflow: hidden;
        position: relative;
      }
      ul {
        /* width: 1600px; */
        height: 250px;
        display: flex;
        /* transform: translateX(-400px); */
        /* transition: all 0.5s; */
      }
      .hastst {
        transition: all 0.5s;
      }
      li {
        width: 400px;
        height: 250px;
      }
      img {
        width: 100%;
        height: 100%;
      }
      button {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
      }
      button:nth-child(2) {
        left: 20px;
      }
      button:nth-child(3) {
        right: 20px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="container" @mouseenter="enter" @mouseleave="leave">
        <ul
          :style="{width: 400 * imgList.length + 'px',transform: 'translateX(' + -400 * num + 'px)'}"
          :class="{hastst: hastst}"
        >
          <li v-for="(item, index) in imgList" :key="index">
            <img :src="item" alt="" />
          </li>
        </ul>
        <button><</button>
        <button @click="rightClick">></button>
      </div>
    </div>

    <script src="../Vue.js"></script>
    <script>
      let timer;
      let timer2;
      new Vue({
        el: "#app",
        data: {
          num: 0,
          hastst: true,
          imgList: [
            "https://img.alicdn.com/imgextra/i3/O1CN01Sf6dER1zbJ3uVQ0lE_!!6000000006732-0-tps-846-472.jpg",
            "https://img.alicdn.com/imgextra/i2/O1CN01XZC8Dd1IUs179sweg_!!6000000000897-2-tps-846-472.png",
            "https://img.alicdn.com/imgextra/i3/O1CN017Z2XUL1nnQ94i2MTP_!!6000000005134-0-tps-846-472.jpg",
            "https://img.alicdn.com/imgextra/i1/O1CN01YGUXOM1k5VKBPo5J3_!!6000000004632-2-tps-846-472.png",
            "https://img.alicdn.com/imgextra/i3/O1CN01Sf6dER1zbJ3uVQ0lE_!!6000000006732-0-tps-846-472.jpg",
          ],
        },
        methods: {
          rightClick() {
            if (!timer2) {
              timer2 = setTimeout(() => {
                this.num++;
                this.hastst = true;
                if (this.num === this.imgList.length - 1) {
                  setTimeout(() => {
                    this.num = 0;
                    this.hastst = false;
                  }, 500);
                }
                timer2 = null;
              }, 500);
            }
          },
          enter() {
            clearInterval(timer);
          },
          leave() {
            timer = setInterval(() => {
              this.rightClick();
            }, 4000);
          },
        },
        mounted() {
          timer = setInterval(() => {
            this.rightClick();
          }, 4000);
        },
      });
    </script>
  </body>
</html>
